package com.travel.server.manager.impl.dal;

import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dao.CustomerInfoDao;
import com.travel.server.manager.model.CustomerInfo;
import com.travel.server.manager.model.CustomerInfoExample;
import com.travel.server.manager.model.dto.CustomerItemDto;
import com.travel.server.manager.model.dto.data.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/7 10:28
 * @Desc:
 */
@Component
@Slf4j
public class CustomerDal {

    @Autowired
    private CustomerInfoDao customerInfoDao;

    public CustomerInfo selectById(Integer id){
        CustomerInfo customerInfo = customerInfoDao.selectByPrimaryKey(id);
        if(customerInfo == null){
            log.error("未查询到用户信息, id {}", id);
            throw new WorkException(WorkCode.RECORD_NOT_EXIST);
        }
        return customerInfo;
    }

    public List<CustomerInfo> selectByExample(CustomerParam param){
        CustomerInfoExample example = makeExample(param);
        example.setOrderByClause("update_time desc");
        return customerInfoDao.selectByExample(example);
    }

    public int batchInsert(List<CustomerInfo> list){
        return customerInfoDao.batchInsert(list);
    }

    public PageDto<CustomerItemDto> selectByPage(CustomerParam param){
        CustomerInfoExample example = makeExample(param);
        int total = customerInfoDao.countByExample(example);
        example.setOrderByClause("update_time desc");
        List<CustomerItemDto> list = customerInfoDao.selectByPage(example);

        PageDto<CustomerItemDto> pageDto = new PageDto<>();
        pageDto.setTotal(total);
        pageDto.setList(list);
        return pageDto;
    }

    public Integer insertOrUpdate(CustomerInfo customerInfo){
        if(customerInfo.getId() == null){
            customerInfoDao.insertSelective(customerInfo);
        }else{
            customerInfoDao.updateByPrimaryKey(customerInfo);
        }

        return customerInfo.getId();
    }

    public List<DealCustomerAnalysisDto> selectDealCustomerOrder(Date lastTime, Date startTime, Date endTime, Integer group){
        return customerInfoDao.selectDealCustomerOrder(lastTime, startTime, endTime, group);
    }

    public List<CustomerDataDto> selectCustomerData(Date startTime, Date endTime, Integer memberId, Integer highMemberId){
        return customerInfoDao.selectCustomerData(startTime, endTime, memberId, highMemberId);
    }

    public List<CustomerDataDto> selectCustomerAnalysis(Date startTime, Date endTime){
        return customerInfoDao.selectCustomerAnalysis(startTime, endTime);
    }

    public List<MemberDealDto> selectMemberDeal(Date startTime, Date endTime, Integer memberId, Integer highMemberId){
        return customerInfoDao.selectMemberDeal(startTime, endTime, memberId, highMemberId);
    }

    public List<CustomerRepeatDto> selectCustomerRepeat(Date startTime, Date endTime){
        return customerInfoDao.selectCustomerRepeat(startTime, endTime);
    }

    public List<CustomerAreaDto> selectDistribution(){
        return customerInfoDao.selectDistribution();
    }

    public List<ProjectCustomerDto> selectProjectCustomer(Integer offset,Integer limit){
        return customerInfoDao.selectProjectCustomer(offset, limit);
    }

    public Integer selectProjectCustomerCount(){
        return customerInfoDao.selectProjectCustomerCount();
    }

    private CustomerInfoExample makeExample(CustomerParam param){
        CustomerInfoExample example = new CustomerInfoExample();
        CustomerInfoExample.Criteria criteria = example.or();
        if(!CollectionUtil.isEmpty(param.getIdList())){
            criteria.andIdIn(param.getIdList());
        }
        if(StringUtils.isNotBlank(param.getName())){
            criteria.andNameLike("%" + param.getName() + "%");
        }
        if(StringUtils.isNotBlank(param.getRealName())){
            criteria.andNameEqualTo(param.getRealName());
        }
        if(StringUtils.isNotBlank(param.getMobile())){
            criteria.andMobileLike("%" + param.getMobile() + "%");
        }
        if(StringUtils.isNotBlank(param.getRealMobile())){
            criteria.andMobileEqualTo(param.getRealMobile());
        }
        if(StringUtils.isNotBlank(param.getWechatNumber())){
            criteria.andWechatNumberLike("%" + param.getWechatNumber() + "%");
        }
        if(StringUtils.isNotBlank(param.getSource())){
            criteria.andSourceEqualTo(param.getSource());
        }
        if(!CollectionUtil.isEmpty(param.getMemberGradeIdList())){
            criteria.andMemberGradeIdIn(param.getMemberGradeIdList());
        }
        if(param.getState() != null){
            criteria.andStateEqualTo(param.getState());
        }
        if(param.getIsMember() != null){
            criteria.andIsMemberEqualTo(param.getIsMember());
        }
        if(param.getOffset() != null && param.getLimit() != null){
            example.setOffset(param.getOffset());
            example.setLimit(param.getLimit());
        }

        return example;
    }

    @Data
    public static class CustomerParam{

        private List<Integer> idList;

        private String name;

        private String realName;

        private String mobile;

        private String realMobile;

        private String wechatNumber;

        private String source;

        private List<Integer> memberGradeIdList;

        private Integer offset;

        private Integer limit;

        private Integer state = 1;

        private Integer isMember;
    }
}
